﻿Imports Zytonic_Framework.Extentions.FileInfoExtentions
Imports System.Windows.Media.Animation
Public Class SettingsWindow
    Public Sub New(Sender As PinControl)
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim color As Color = DirectCast(MyProperties.DockWindow.PinStackPanel.Background, SolidColorBrush).Color

        InsideBorder.Background = DirectCast(MyProperties.DockWindow.PinStackPanel.Background, SolidColorBrush)
        ArgumentsTextbox.Background = InsideBorder.Background
        FilenameTextbox.Background = ArgumentsTextbox.Background
        SearchButton.Background = FilenameTextbox.Background
        Dim FileInfo As New IO.FileInfo(Sender.PinData.Filename)
        Dim ImageExtentions As String() = {".png", ".bmp", ".jpg", ".gif", ".tif"}
        Dim FileExtention As String = FileInfo.Extension.ToLower
        Dim URI As Uri = Nothing

        If ImageExtentions.Contains(FileExtention) Then
            URI = New Uri(Sender.PinData.Filename, UriKind.RelativeOrAbsolute)
        Else
            URI = New Uri(Sender.PinData.CachedIconFilename, UriKind.RelativeOrAbsolute)
        End If

        Dim BI As New BitmapImage
        BI.BeginInit()
        BI.CacheOption = BitmapCacheOption.None
        BI.UriSource = URI
        BI.EndInit()
        PinImage.Source = BI

        FilenameTextbox.Text = FileInfo.FullName

        If FileInfo.isDirectory = True Then
            PinName.Text = FileInfo.FullName.Remove(0, FileInfo.FullName.LastIndexOf("\") + 1)
            Dim T As New System.Threading.Thread( _
                Sub()
                    'Load Directory Information
                    Dim DirectorySize As Long = 0, FileCount As Integer = 0, Above1GB As Boolean = False
                    Dim DisplaySize As String = String.Empty
                    Dim DirInfo As New IO.DirectoryInfo(FileInfo.FullName)
                    SetDisplayInfo({"Type: Directory", _
                                    "Size: Loading...", _
                                    "File Count: Loading...", _
                                    "Last Write: " & DirInfo.LastWriteTime.ToString, _
                                    "Date Created: " & DirInfo.CreationTime.ToString})
                    Try
                        For Each File As IO.FileInfo In DirInfo.GetFiles("*.*", IO.SearchOption.AllDirectories)
                            DirectorySize += File.Length
                            FileCount += 1
                            If DirectorySize > 1073741824 Then
                                DisplaySize = Math.Round(DirectorySize / 1024 / 1024 / 1024, 2) & "GB+ (Over 1GB)"
                                Exit For
                            End If
                        Next
                    Catch ex As IO.IOException
                        DisplaySize = "Failed to get size."
                    End Try
                    If DisplaySize = String.Empty Then DisplaySize = Math.Round(DirectorySize / 1024 / 1024, 2) & "MB"
                    SetDisplayInfo({"Type: Directory", _
                                    "Size: " & DisplaySize, _
                                    "File Count: " & FileCount, _
                                    "Last Write: " & DirInfo.LastWriteTime.ToString, _
                                    "Date Created: " & DirInfo.CreationTime.ToString})
                End Sub)
            T.Start()
            AddHandler SearchButton.Click, _
          Sub()
              Dim FolderDialog As New Forms.FolderBrowserDialog With {.ShowNewFolderButton = True, .Description = "Select a new path."}
              FolderDialog.ShowDialog()
              If FolderDialog.SelectedPath <> Nothing Then
                  Dim newPin As New PinControl(FolderDialog.SelectedPath)
                  Dim Index As Integer = FindPinIndex(Sender.PinData.Filename)
                  MyProperties.DockWindow.PinStackPanel.Children.RemoveAt(Index)
                  MyProperties.DockWindow.PinStackPanel.Children.Insert(Index, newPin)
                  Dim newWindow As New SettingsWindow(newPin)
                  newWindow.Show()
                  Me.Close()
              End If
          End Sub
        Else
            ArgumentsTextbox.Text = Sender.PinData.Arguments
            ArgumentsTextbox.IsEnabled = True
            PinName.Text = IO.Path.GetFileNameWithoutExtension(FileInfo.FullName)
            SetDisplayInfo({"Type: File (" & FileInfo.Extension & ")", _
                            "Size: " & Math.Round(FileInfo.Length / 1024 / 1024, 2) & "MB", _
                            "Last Write: " & FileInfo.LastWriteTime.ToString, _
                            "Date Created: " & FileInfo.CreationTime.ToString, _
                            "Read-Only: " & FileInfo.IsReadOnly.ToString})
            AddHandler SearchButton.Click, _
                Sub()
                    Dim OpenFileDialog As New Forms.OpenFileDialog With {.CheckFileExists = True, .Title = "Select a new file.", .Multiselect = False}
                    OpenFileDialog.Filter = "Applications, Shortcuts, & Images|*.exe;*.png;*.bmp;*.jpg;*.gif;*.tif;*.lnk;"
                    OpenFileDialog.ShowDialog()
                    If OpenFileDialog.FileName <> Nothing Then
                        Dim newPin As New PinControl(OpenFileDialog.FileName)
                        Dim Index As Integer = FindPinIndex(Sender.PinData.Filename)
                        MyProperties.DockWindow.PinStackPanel.Children.RemoveAt(Index)
                        MyProperties.DockWindow.PinStackPanel.Children.Insert(Index, newPin)
                        Dim newWindow As New SettingsWindow(newPin)
                        newWindow.Show()
                        Me.Close()
                    End If
                End Sub
            AddHandler Me.Closing, _
                Sub()
                    For Each Pin As PinData In Properties.MyPins.Pins
                        If Pin.Filename = Sender.PinData.Filename Then
                            Pin.Arguments = ArgumentsTextbox.Text
                        End If
                    Next
                End Sub
        End If
        Me.Opacity = 0.0
        Dim doubleAnim As New DoubleAnimation(0.0, 1.0, New Duration(TimeSpan.FromSeconds(0.5)))
        Me.BeginAnimation(FrameworkElement.OpacityProperty, doubleAnim)
    End Sub
    Private Sub SetDisplayInfo(Information As String())
        Dispatcher.Invoke(Sub() AppInfo.Text = String.Join(vbNewLine, Information))
    End Sub
    Private Sub MyBorder_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles InsideBorder.MouseDown
        Try : Me.DragMove() : Catch : End Try
    End Sub
    Private Sub CloseBorder_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles CloseLabel.MouseDown
        Dim doubleAnim As New DoubleAnimation(1.0, 0.01, New Duration(TimeSpan.FromSeconds(0.35))) _
            With {.EasingFunction = New CircleEase With {.EasingMode = EasingMode.EaseInOut}}
        AddHandler doubleAnim.Completed, Sub() Me.Close()
        Me.BeginAnimation(FrameworkElement.OpacityProperty, doubleAnim)
    End Sub
    Private Function FilterToNewLineByCharacter(ByVal str As String, Place As Integer, Optional FillWhitespace As Boolean = False) As String
        Dim Words As Integer
        Dim FinishedString = ""
        For Words = 1 To str.Length Step Place
            FinishedString += Mid(str, Words, (Words + Place) - 1) & Environment.NewLine
            If FillWhitespace = True Then
                For i = 0 To Place
                    FinishedString += " "
                Next
            End If
        Next
        Return Mid(FinishedString, 1, 90) & If(str.Length > 90, "...", "")
    End Function
End Class
